package cn.gzsxt.music.mapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;

import cn.gzsxt.music.query.QueryObject;
import cn.gzsxt.music.query.SongQueryObject;
import cn.gzsxt.music.util.GetSqlUtil;

@Mapper
public class MapperSqlProvider {
	
	private String tableName = "mc_song";
	
	private String multiTables = " mc_song tbl, mc_singer s, mc_cd c, mc_lang l ";
	
	private String multiConditions = " tbl.singer_id = s.singer_id and tbl.cd_id = c.cd_id and tbl.lang_id = l.lang_id";
	
	private String selectSql = "select tbl.*, s.singer_name, c.cd_name, l.lang_name ";
	
	private String selectCount = "select count(*) ";
	
	@Autowired
	private GetSqlUtil sqls;

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String addSql(Map<String, Object> params) {
		StringBuffer sql = new StringBuffer();
		StringBuffer value = new StringBuffer();
		sql.append( "insert into "+tableName+" (");
		Set<String> keySet = params.keySet();
		for (String key : keySet) {
			if (key.equals("song_id")) {
				continue;
			}
			try {
				if (StringUtils.isNotBlank((String) params.get(key))) {
					sql.append(key+",");
					value.append("#{"+key+"},");				
				}				
			} catch (Exception e) {
				//异常的话就不添加
				continue;
			}			
		}
		sql.deleteCharAt(sql.length()-1);
		value.deleteCharAt(value.length()-1);
		sql.append(") values ("+value+")");
		System.out.println(sql);
		return sql.toString();
	}
	
	public String updSql(Map<String, Object> params) {
		StringBuffer sql = new StringBuffer();
		sql.append( "update "+tableName+" set ");
		Set<String> keySet = params.keySet();
		for (String key : keySet) {
			if (key.equals("song_id")) {
				continue;
			}
			try {
				if (StringUtils.isNotBlank((String) params.get(key))) {	
					sql.append(key+"= #{"+key+"},");
				}				
			} catch (Exception e) {
				//异常的话就不添加
				continue;
			}			
		}
		sql.deleteCharAt(sql.length()-1);
		sql.append(" where song_id = #{song_id}");
		return sql.toString();
	}
	
	public String delSql(String ids) {
		return "delete from "+tableName+" where song_id in ("+ids+")";
	}
	
	public String selectByCondition(Map<String, Object> params) {
		StringBuffer sql = new StringBuffer();
		sql.append(selectSql+"from"+multiTables+"where"+multiConditions);
		sql.append(select(params));
		sql.append(" limit #{beginIndex},#{rows}");
		System.out.println(sql);
		return sql.toString();
	}
	
	public String selectTotalByCondition(Map<String, Object> params) {
		StringBuffer sql = new StringBuffer();
		sql.append(selectCount+"from"+multiTables+"where"+multiConditions);
		sql.append(select(params));
		System.out.println(sql);
		return sql.toString();		
	}
	
	public StringBuffer select(Map<String, Object> params) {
		StringBuffer sql = new StringBuffer();
		sql.append("");
		if (params.containsKey("song_name")) {
			sql.append(" and song_name like CONCAT( '%',#{song_name},'%')");
		}
		if (params.containsKey("cd_name")) {
			sql.append(" and cd_name like CONCAT( '%',#{cd_name},'%')");
		}
		if (params.containsKey("singer_name")) {
			sql.append(" and singer_name like CONCAT( '%',#{singer_name},'%')");
		}
		if (params.containsKey("publish_begin")) {
			sql.append(" and song_publish > #{publish_begin}");
		}
		if (params.containsKey("publish_end")) {
			sql.append(" and song_publish < #{publish_end}");
		}
		if (params.containsKey("lang_id")) {
			sql.append(" and l.lang_id = #{lang_id}");
		}
		System.out.println(sql);
		return sql;
	}
	
}
